import { ref } from "vue"
import { defineStore } from 'pinia'


export default defineStore('mainSearchStore', ()=>
  {

    // 最大历史记录个数
    const maxCount = 15;

    // 搜索引擎
    const engines = {
      
      "baidu": {
        key: "baidu",
        text: "百度",
        url:"https://www.baidu.com/s?wd=",
      },
    
      "bing": {
        key: "bing",
        text: "必应",
        url:"https://cn.bing.com/search?q=",
      },
    
    
      // {
      //   key: "google",
      //   name: "google",
      //   url:"",
      // },
    
      // {
      //   key: "yandex",
      //   name: "yandex",
      //   url:"",
      // },
    };

    const selects = Object.values(engines);//搜索引擎列表
    const select = ref(selects[0].key); //已选搜索引擎
    // console.log(selects, select.value);
    

    // const kwds = ref(new Set(['友和网'])); //历史搜索词
    const kwds = ref([]); //历史搜索词


    const kwd = ref("");//当前搜索词


    // 生成搜索url
    function setSearchUrl(_kwd){
      if(!_kwd) _kwd = kwd.value;
      let engine = engines[select.value];
      return engine.url + _kwd;
    }



    return {
      maxCount,
      engines, 
      selects, 
      select, 
      kwds, 
      kwd,

      setSearchUrl,
    };


  }, 



  {
    persist:{
      storage: localStorage,
      paths: ['kwds', 'select'],//必需与state一致
    }
  }

);

